package com.jztd.servone.invoice.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.xbatis.core.mybatis.mapper.context.Pager;
import cn.xbatis.datasource.routing.DS;
import com.jztd.platfrom.base.exception.BusinessException;
import com.jztd.platfrom.base.util.UserUtil;
import com.jztd.servone.emun.InvoiceStatusEnum;
import com.jztd.servone.invoice.dto.InvoiceRecordDTO;
import com.jztd.servone.invoice.mapper.InvoiceRecordMapper;
import com.jztd.servone.invoice.param.BasePageParam;
import com.jztd.servone.invoice.po.BusinessConfig;
import com.jztd.servone.invoice.po.InvoiceRecord;
import com.jztd.servone.invoice.service.BusinessConfigService;
import com.jztd.servone.invoice.service.InvoiceRecordService;
import com.jztd.servone.invoice.service.OrderServiceFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@DS("invoice")
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class invoiceRecordServiceImpl implements InvoiceRecordService {
    private final InvoiceRecordMapper invoiceRecordMapper;
    private final BusinessConfigService businessConfigService;
    private final OrderServiceFactory orderServiceFactory;
    Snowflake snowflake = IdUtil.getSnowflake(1, 1);
    @Override
    public InvoiceRecord getInvoiceRecordById(String id) {
        return invoiceRecordMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addInvoiceRecord(InvoiceRecord invoiceRecord) {
        invoiceRecord.setId(snowflake.nextIdStr());
        invoiceRecord.setIsDeleted(0);
        invoiceRecord.setCreateBy(UserUtil.getUserId());
        invoiceRecord.setCreateTime(new Date());
        invoiceRecord.setInvoiceIssueKindCode("82");
        Boolean bool = invoiceRecordMapper.save(invoiceRecord) > 0;
        if(bool){
            BusinessConfig businessConfig = businessConfigService.getBusinessConfig(invoiceRecord.getTenant());
             orderServiceFactory.getOrderService(businessConfig.getType()).updateOrderInfo(businessConfig,invoiceRecord.getInvoiceCode(),invoiceRecord.getInvoiceStatus());
        }
        return bool;
    }

    @Override
    public Pager<InvoiceRecordDTO> getInvoiceRecordList(BasePageParam pageParam) {
        Pager<InvoiceRecord> page = new Pager<>(pageParam.getNumber(), pageParam.getSize());
        page = invoiceRecordMapper.paging(page, where -> {
            where.eq(InvoiceRecord::getCreateBy, UserUtil.getUserId())
                    .eq(InvoiceRecord::getTenant, UserUtil.getTenant());
        });
        Pager<InvoiceRecordDTO> page2 = new Pager<>(pageParam.getNumber(), pageParam.getSize());
        List<InvoiceRecordDTO> data = Convert.toList(InvoiceRecordDTO.class, page.getResults());
        page2.setResults(data);
        page2.setTotal(page.getTotal());
        return page2;
    }

    @Override
    public Boolean updateInvoiceRecord(InvoiceRecord invoiceRecord) {
        invoiceRecord.setUpdateBy(UserUtil.getUserId());
        invoiceRecord.setUpdateTime(new Date());
        return invoiceRecordMapper.update(invoiceRecord) > 0;
    }

    @Override
    public void checkInvoiceResults(String invoiceCode) {
       List<InvoiceRecord> invoiceRecords = invoiceRecordMapper.list(where -> {
           where.eq(InvoiceRecord::getTradeNo, invoiceCode);
           where.in(InvoiceRecord::getInvoiceStatus, InvoiceStatusEnum.getPassStatusList());
        });
       if(ObjectUtil.isNotEmpty(invoiceRecords)){
           throw new BusinessException("订单已开具发票，不能重复开票");
       }
    }
}
